﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using VISS.API.Users;
using NCT.Framework;
using System.Collections.Generic;

namespace VISS.Web.Frontend.Modules.User.Profile {

    public enum ESearchViewType {
        Thumb = 1,
        Detail = 2
    }

    public partial class SearchPage : System.Web.UI.Page {

        protected ESearchViewType ViewType = ESearchViewType.Thumb;
        protected int TotalRecord = 0;
        protected string DisplayRecord;
        protected string Paging;
        protected string Url;        
        protected bool CanDoAction;

        protected void Page_Load(object sender, EventArgs e) {
            UserContext userContext = LoginFactory.GetCurrentUser();
            if (userContext == null) {
                Response.Redirect("/login");
                return;
            }
            switch (ConvertUtils.ToString(Request.QueryString["viewtype"])) {
                case "thumb":
                    ViewType = ESearchViewType.Thumb;
                    break;
                case "detail":
                    ViewType = ESearchViewType.Detail;
                    break;
                default:
                    ViewType = ESearchViewType.Thumb;
                    break;
            }

            int page;
            short gender;
            short cityID;
            short jobID;

            int fromAge = ConvertUtils.ToInt(Request.QueryString["fromage"], -1);
            int toAge = ConvertUtils.ToInt(Request.QueryString["toage"], -1);
            page = ConvertUtils.ToInt(Request.QueryString["p"], 1);
            gender = ConvertUtils.ToShort(Request.QueryString["gender"], -1);
            cityID = ConvertUtils.ToShort(Request.QueryString["city"], -1);
            jobID = ConvertUtils.ToShort(Request.QueryString["job"], -1);

            int maxPage = 0;
            int pageSize = 0;
            if (ViewType == ESearchViewType.Thumb) {
                pageSize = AppSettings.PageSizeOfSearchThumb;
            }
            else {
                pageSize = AppSettings.PageSizeOfSearchDetail;
            }

            maxPage = AppSettings.SearchDisplayRecordLimit / pageSize;
            if (AppSettings.SearchDisplayRecordLimit % pageSize != 0) {
                maxPage++;
            }

            if (page <= maxPage) {
                IUserGeneralProfile _objectGeneral = StructureMapFactory.GetInstance<IUserGeneralProfile>();

                string queryString = AppSettings.RootURL + "profile/search?";
                int count = 0;

                UserSrhEnt srhEnt = new UserSrhEnt();

                if (gender != -1) {
                    srhEnt.Gender = (EUserGender)gender;
                    if (srhEnt.Gender == EUserGender.Female) {
                        queryString += "gender=0";
                        count++;
                    }
                    else if (srhEnt.Gender == EUserGender.Male) {
                        queryString += "gender=1";
                        count++;
                    }
                }
                else {
                    srhEnt.Gender = EUserGender.Unknown;
                }

                srhEnt.CityID = cityID;
                if (srhEnt.CityID > 0) {
                    if (count > 0) {
                        queryString += "&";
                    }
                    queryString += "city=" + Request.QueryString["city"];
                    count++;
                }
                srhEnt.JobID = jobID;
                if (srhEnt.JobID > 0) {
                    if (count > 0) {
                        queryString += "&";
                    }
                    queryString += "job=" + Request.QueryString["job"];
                    count++;
                }

                int temp;
                if ((fromAge > toAge) && fromAge > 0 && toAge > 0) {
                    temp = fromAge;
                    fromAge = toAge;
                    toAge = temp;
                }

                if (fromAge > 0) {
                    if (fromAge < 18) {
                        fromAge = 18;
                    }
                    if (count > 0) {
                        queryString += "&";
                    }
                    count++;
                    queryString += "fromage=" + fromAge.ToString();

                    srhEnt.ToBirthDate = new DateTime(DateTime.Now.Year - fromAge, 12, 31);
                }
                if (toAge > 0) {
                    if (toAge > 90) {
                        toAge = 90;
                    }
                    if (count > 0) {
                        queryString += "&";
                    }
                    count++;
                    queryString += "toage=" + toAge.ToString();
                    srhEnt.FromBirthDate = new DateTime(DateTime.Now.Year - toAge, 1, 1);
                }

                if (count == 0) {
                    Response.Redirect("/error?t=" + (int)EErrorType.Http404);
                    return;
                }

                Url = queryString;
                if (ViewType == ESearchViewType.Thumb) {
                    queryString += "&viewtype=thumb";
                }
                else {
                    queryString += "&viewtype=detail";
                }

                List<UserSrhResEnt> result = null;
                
                result = _objectGeneral.QuickSearch(srhEnt, EUserQuickSearchOrderType.LastLogin, page, pageSize, out TotalRecord);
                
                if (TotalRecord <= AppSettings.SearchDisplayRecordLimit) {
                    DisplayRecord = TotalRecord.ToString();
                }
                else {
                    DisplayRecord = AppSettings.SearchDisplayRecordLimit + "+";
                }
                idSearchDetailResult.SearchResult = idSearchThumbResult.SearchResult = result;
                
                DefaultMasterPage masterPage = (DefaultMasterPage)this.Master;
                if (srhEnt.Gender != EUserGender.Unknown) {
                    masterPage.Gender = srhEnt.Gender;
                    masterPage.UseDefaultSearchCondition = false;
                }
                else {
                    masterPage.UseDefaultSearchCondition = true;
                }

                if ((userContext.Status & EUserStatus.Verified) == EUserStatus.Verified) {
                    CanDoAction = true;
                    idSearchDetailResult.CanDoAction = idSearchThumbResult.CanDoAction = true;
                }
                else {
                    CanDoAction = false;
                    idSearchDetailResult.CanDoAction = idSearchThumbResult.CanDoAction = false;
                }
                
                masterPage.CityID = cityID;
                masterPage.JobID = jobID;
                masterPage.FromAge = fromAge;
                masterPage.ToAge = toAge;

                Paging pager;
                if (TotalRecord <= AppSettings.SearchDisplayRecordLimit) {
                    pager = new Paging(TotalRecord);
                }
                else {
                    pager = new Paging(AppSettings.SearchDisplayRecordLimit);
                }
                pager.PerPage = pageSize;
                pager.QueryString = queryString;
                pager.Param = "p";
                pager.ShowMore = false;
                pager.LinkFormatTemplate = "<div class=\"page\">{0}</div>";
                pager.SelectedPageFormatTemplate = "<div class=\"page selected-page\">{0}</div>";

                Paging = pager.BuildPaging();
            }
            else {
                idSearchDetailResult.SearchResult = idSearchThumbResult.SearchResult = new List<UserSrhResEnt>();
                Paging = string.Empty;
            }

            // SEO
            IHeadSeo seo = this.Master as IHeadSeo;
            seo.Title = StringResources.SEO.TitleSearch + " | " + userContext.FullName + " | " + StringResources.SEO.TitleDefault;
            seo.Description = StringResources.SEO.DescriptionDefault;
            seo.Keywords = StringResources.SEO.KeywordsDefault;
        }
    }
}
